package com.woniu.hospital.controller;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniu.hospital.domain.EquipemntUse;
import com.woniu.hospital.service.IEquipemntService;
import com.woniu.hospital.domain.EquipemntUseVo;
import com.woniu.hospital.service.IEquipemntUseService;
import com.woniu.hospital.service.UserService;
import com.woniu.user.domain.Doctor;
import com.woniu.hospital.service.IPatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 设备调用表Controller
 *
 * @author wolf-six
 * @date 2023-02-15
 */
@RestController
@RequestMapping("/equipemnt_use")
public class EquipemntUseController extends BaseController
{
    @Autowired
    private IEquipemntUseService equipemntUseService;
    @Autowired
    private IPatientService iPatientService;

    @Autowired
    private UserService userService;

    @Autowired
    private IEquipemntService equipemntService;

    /**
     * 查询设备调用表列表
     */
    @RequiresPermissions("hospital:equipemnt_use:list")
    @GetMapping("/list")
    public TableDataInfo list(EquipemntUse equipemntUse)
    {
        startPage();
        List<EquipemntUse> list = equipemntUseService.selectEquipemntUseList(equipemntUse);
        for (EquipemntUse item:list) {
            String doctorName = userService.selDoctorById(item.getDocterId()).getDoctorName();
            String patientName = userService.getPatientById(item.getPatientId()).getPatientName();
            String equipmentName = equipemntService.selectEquipemntByEquipmentId(item.getEquipmentId()).getEquipmentName();
            item.setDoctorName(doctorName);
            item.setPatientName(patientName);
            item.setEquipmentName(equipmentName);
        }

        return getDataTable(list);
    }


    @RequiresPermissions("hospital:equipemnt_use:list")
    @GetMapping("/listyan")
    public TableDataInfo listYan(EquipemntUseVo equipemntUseVo)
    {
        startPage();
        EquipemntUse equipemntUse = new EquipemntUse();
        equipemntUse.setEquipmentuseTime(equipemntUseVo.getEquipmentuseTime());
        equipemntUse.setEquipmentuseReturntime(equipemntUseVo.getEquipmentuseReturntime());
        equipemntUse.setEquipmentuseMoney(equipemntUseVo.getEquipmentuseMoney());

        if(equipemntUseVo.getDocterName() != null && equipemntUseVo.getDocterName() != ""){
            equipemntUse.setDocterId(iPatientService.getDoctorIdByName(equipemntUseVo.getDocterName()).getDoctorId());
        }
        if(equipemntUseVo.getPatientName() != null && equipemntUseVo.getPatientName() != ""){
            equipemntUse.setPatientId(iPatientService.getPatientByNameYan(equipemntUseVo.getPatientName()).getPatientId());
        }

        List<EquipemntUse> list = equipemntUseService.selectEquipemntUseList(equipemntUse);
        List<EquipemntUseVo> equipemntUseVos = new ArrayList<>();
        for (EquipemntUse use : list) {
            EquipemntUseVo equipemntUseVo1 = new EquipemntUseVo();
            equipemntUseVo1.setEquipmentuseId(use.getEquipmentuseId());
            equipemntUseVo1.setEquipmentuseTime(use.getEquipmentuseTime());
            equipemntUseVo1.setEquipmentuseReturntime(use.getEquipmentuseReturntime());
            equipemntUseVo1.setEquipmentuseMoney(use.getEquipmentuseMoney());
            equipemntUseVo1.setEquipmentId(use.getEquipmentId());
            equipemntUseVo1.setPatientId(use.getPatientId());
            equipemntUseVo1.setPatientName(iPatientService.selectById(use.getPatientId()).getPatientName());
            equipemntUseVo1.setDocterId(use.getDocterId());
            equipemntUseVo1.setDocterName(iPatientService.selectDoctoeNameById(use.getDocterId()));


            equipemntUseVos.add(equipemntUseVo1);
        }
        return getDataTable(equipemntUseVos);
    }

    /**
     * 导出设备调用表列表
     */
    @RequiresPermissions("hospital:equipemnt_use:export")
    @Log(title = "设备调用表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipemntUse equipemntUse)
    {
        List<EquipemntUse> list = equipemntUseService.selectEquipemntUseList(equipemntUse);
        ExcelUtil<EquipemntUse> util = new ExcelUtil<EquipemntUse>(EquipemntUse.class);
        util.exportExcel(response, list, "设备调用表数据");
    }

    /**
     * 获取设备调用表详细信息
     */
    @RequiresPermissions("hospital:equipemnt_use:query")
    @GetMapping(value = "/{equipmentuseId}")
    public AjaxResult getInfo(@PathVariable("equipmentuseId") Long equipmentuseId)
    {
        return success(equipemntUseService.selectEquipemntUseByEquipmentuseId(equipmentuseId));
    }

    /**
     * 新增设备调用表
     */
    @RequiresPermissions("hospital:equipemnt_use:add")
    @Log(title = "设备调用表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquipemntUse equipemntUse)
    {
        return toAjax(equipemntUseService.insertEquipemntUse(equipemntUse));
    }

    /**
     * 修改设备调用表
     */
    @RequiresPermissions("hospital:equipemnt_use:edit")
    @Log(title = "设备调用表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipemntUse equipemntUse)
    {
        return toAjax(equipemntUseService.updateEquipemntUse(equipemntUse));
    }

    /**
     * 删除设备调用表
     */
    @RequiresPermissions("hospital:equipemnt_use:remove")
    @Log(title = "设备调用表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{equipmentuseIds}")
    public AjaxResult remove(@PathVariable Long[] equipmentuseIds)
    {
        return toAjax(equipemntUseService.deleteEquipemntUseByEquipmentuseIds(equipmentuseIds));
    }
}
